home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 9 / Night Owl CD-ROM (NOPV9) (Night Owl Publisher) (1993).ISO / 009a / snpd0493.zip / EXCEPT.DOC < prev    next >
Text File  |  1993-04-05  |  9KB  |  185 lines

  1. USER-GENERATED EXCEPTIONS IN GENERAL
  2. ------------------------------------
  3.  
  4. MS-DOS users can generate exceptions by means of two separate
  5. mechanisms, Ctrl-C and Ctrl-Break. Although these are often treated
  6. the same, they are actually handled in subtly different manners. The
  7. difference in the way these are processed allows a great deal of
  8. flexibility in allowing users to interrupt an executing program.
  9.  
  10. When a program is executed, the Ctrl-C Interrupt 23h is set up to
  11. point to a default error handler. This handler is called whenever a
  12. Ctrl-C character is detected in the keyboard input buffer. When a
  13. program terminates in any way, MS-DOS resets the Interrupt 23h vector
  14. to its default state. Note that the Ctrl-C character in the input
  15. buffer is only recognized - and an Interrupt 23h generated - when
  16. retrieving characters from the buffer and if BREAK ON is set.
  17.  
  18. The Ctrl-Break Interrupt 1Bh works somewhat differently, though
  19. usually in concert with Interrupt 23h. Whenever the ROM BIOS detects
  20. the Ctrl-Break key combination, the keyboard buffer is flushed and a
  21. Ctrl-C key combination is stuffed in place of the previous contents.
  22. This Ctrl-C will later be detected and processed by Interrupt 23h.
  23. Ctrl-Break processing therefore offers more immediate response than
  24. Ctrl-C processing if the default action is overridden.
  25.  
  26. Several caveats are in order here, however. First is the fact that,
  27. unlike Interrupt 23h, MS-DOS does not restore the default state of
  28. Interrupt 1Bh upon program termination. Second is that while Ctrl-C
  29. processing is standardized among the various machines utilizing both
  30. MS-DOS and PC-DOS, Ctrl-Break processing is much less standardized.
  31. Finally, since processing either ultimately relies on trapping
  32. Ctrl-C, either may be ignored for a long period because of the way
  33. that Ctrl-C is detected.
  34.  
  35.  
  36. HANDLING USER-GENERATED EXCEPTIONS
  37. ----------------------------------
  38.  
  39. DOS's default Ctrl-C handler is triggered whenever the Ctrl-C
  40. character is detected in the input buffer. DOS's response is to
  41. simply close all files which were opened using handle functions and
  42. to terminate the program. The limitations of this approach and the
  43. desirability of providing your own exception processing is obvious.
  44.  
  45. An equally obvious solution to the default Ctrl-C handler's problems
  46. is to explicitly do your own Ctrl-C exception processing. CCTRAP.ASM
  47. installs and de-installs you own customized exception handler. Note
  48. that the code is written to accept the address of a function specified
  49. with an explicit segment and offset.
  50.  
  51. Also note that an explicit de-installation function is provided
  52. despite the fact that DOS restores the default Int 23h vector upon
  53. program termination. The reason this is provided is that you should
  54. always de-install a Ctrl-C interrupt trap before you spawn a child
  55. process. Within your program, if you need to spawn such a process
  56. through any mechanism other than spawning a subordinate shell (more
  57. on this in a second), you should explicitly de-install your interrupt
  58. handlers and re-install them when the subordinate process returns. As
  59. noted, this is unnecessary when the subordinate process is a DOS
  60. shell such as COMMAND.COM, since the shell will reset the interrupts
  61. to their defaults during execution.
  62.  
  63. Ctrl-Break processing is much more problematical, though potentially
  64. more powerful. The first problem to deal with is how to assure that
  65. the default Int 1Bh Ctrl-Break handler will be restored upon program
  66. termination. The de-installation function therefore becomes mandatory
  67. in this context rather than optional as in the case of the Int 23h
  68. handler. CBTRAP.ASM shows a sample Ctrl-Break handler. Since Ctrl-Break
  69. processing is much less standardized than Ctrl-C processing, the
  70. safest way to deal with it is to simply set a flag, "cbrcvd", which
  71. informs your program that a Ctrl-Break has been received. Your
  72. program may then poll this flag and take appropriate actions at
  73. "safe" times within your program.
  74.  
  75.  
  76. WHERE THE CARET-C COMES FROM
  77. ----------------------------
  78.  
  79. There's still nothing new here and nothing to prevent the ugly "^C"
  80. being printed to the screen. This is because it is actually printed
  81. by the BIOS during Int 9 processing, long before DOS ever sees it.
  82. What this means is that even though the code in CCTRAP.ASM and
  83. CBTRAP.ASM is fine, it still only provides a framework for solving
  84. our problem.
  85.  
  86. TRAPFLAG.ASM is the final trick to banish the "^C". The actual ISR
  87. has to muck around quite a bit with the keyboard hardware, as is to
  88. be expected of an Int 09h replacement. Whenever a Ctrl-C or Ctrl-
  89. Break is detected, it is trapped, and our exception handler called in
  90. place of the original Int 09h handler, after discarding the trapped
  91. key codes.
  92.  
  93. Referring to TRAPFLAG.ASM, note that since I need to trap both Ctrl-C
  94. and Ctrl-Break, I adopt the flag approach introduced in CBTRAP.ASM
  95. for dealing with both Ctrl-C and Ctrl-Break processing. Now, rather
  96. than supplying an explicit exception vector, I merely set a global
  97. flag to inform me if either exception has occurred and accept the
  98. responsibility of processing the exceptions within the body of my
  99. program. I've added an extra bit of versatility here by posting
  100. different non-zero values to the flag, "ccrcvd" depending on whether
  101. the exception was a Ctrl-C or Ctrl-Break.
  102.  
  103. TRAPDEMO.C is a short C program demonstrating the use of the combined
  104. Ctrl-C/Ctrl-Break handler. Using this approach, your carefully
  105. crafted screens need never more be cluttered with the "^C" uglies.
  106.  
  107.  
  108. SYSTEM-GENERATED EXCEPTIONS
  109. ---------------------------
  110.  
  111. It's usually desirable, in any professional-looking program, to
  112. explicitly trap the Int 24h critical error interrupt to process
  113. system-generated exceptions. CERRINST.ASM is a portable critical
  114. error handler installation program functionally equivalent to the
  115. [_]hardxxx() package in Borland C++ and Microsoft C++ compilers, and
  116. the ceror_xxx() package in Zortech C++.
  117.  
  118. It's obvious that writing code to intercept DOS critical error
  119. exceptions is just as simple as intercepting Ctrl-C or Ctrl-Break
  120. exceptions. The real challenge in writing critical error handlers is
  121. in interpretation of the nature of the exception.
  122.  
  123. The critical error handler requires more information in order to
  124. decide what action to take than does a Ctrl-C handler. All of this
  125. information is passed in the CPU's registers. Just like a typical
  126. compiler vendor's critical error handler, CERRINST.ASM will simply
  127. pass these registers and leave their interpretation to you. In
  128. CERRINST.ASM, the information required for intelligent critical error
  129. processing is posted in 4 global variables, cedevdvr, cetype,
  130. ceerror, and cereturn.
  131.  
  132. Next you need to determine what your program requires of a critical
  133. error handler. CERRTRAP.ASM is a skeletal, yet robust critical error
  134. function which may be called from the handler in CERRINST.ASM.
  135. CERRTRAP.ASM assumes you have set up the following specific error
  136. handlers in the global variables provided:
  137.  
  138. FAT error               (*FAT_err)();
  139. Disk read error         (*read_err)()
  140. Disk write error        (*write_err)()
  141. Terminal error          (*term_err)(),
  142. Printer out of paper    (*no_paper)(),
  143. All other errors        (*fixup_ret)(),
  144.  
  145. In the case of an unrecognized error, fixup_ret() is called. A simple
  146. skeleton for this function would be:
  147.  
  148. #include <dos.h>        /* for _osmajor                                 */
  149. extern int exerr;       /* DOS extended error posted by CERRTRAP.ASM    */
  150. extern int rmvbl;       /* removable media flag posted by CERRTRAP.ASM  */
  151. extern int locus;       /* extened error locus posted by CERRTRAP.ASM   */
  152. extern int class;       /* extened error class posted by CERRTRAP.ASM   */
  153. extern int suggest;     /* suggested action posted by CERRTRAP.ASM      */
  154.  
  155. int fixup_ret(void)
  156. {
  157.         if (2 < _osmajor)
  158.         {
  159.                 /* analyze DOS extended error information */
  160.  
  161.                 return appropriate_error_code;
  162.         }
  163.  
  164.         /* cleanup */
  165.                 
  166.         return 2;       /* abort */
  167. }
  168.  
  169. In customing your specific critical error handler functions, there
  170. are several important restrictions to keep in mind. The first is that
  171. no DOS system services may be requested other than Interrupt 21h
  172. functions 01h-0Ch (character I/O), 30h (get DOS version number), and
  173. 59h (get extended error information). All registers except AL must be
  174. preserved since DOS sets them up for processing Retry returns prior
  175. to invoking the critical error interrupt.
  176.  
  177. Finally, the handler must return with an IRET instruction, passing a
  178. return code in AL to tell DOS what to do next. The available codes
  179. and their actions under various DOS versions are:
  180.  
  181.         0 - Ignore
  182.         1 - Retry
  183.         2 - Abort
  184.         3 - Fail (DOS 3.3 and later)
  185.